import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import FullPage from "@/layout/components/FullPage.vue";


/* Router Modules */

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/login/register'),
    name: 'register',
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/adminLogin',
    component: () => import('@/views/login/adminLogin'),
    name: 'adminLogin',
    hidden: true,
  },
  {
    path: '/backLogin',
    component: () => import('@/views/login/backLogin'),
    name: 'backLogin',
    hidden: true,
  },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index/index'),
        name: 'index',
        meta: { title: 'home', icon: 'el-icon-c-scale-to-original', },
        hidden: true
      },
    ]
  },
  // 仓库warehouse
  {
    path: '/warehouse',
    component: Layout,
    redirect: '/warehouse/List',
    meta: {title:'warehouseManagement',roles:['admin']},
    children: [
      {
        path: '/warehouse/warehouseList',
        component: () => import('@/views/warehouse/warehouseList'),
        name: 'warehouseList',
        meta: { title: '海外仓列表', roles: ['admin']},
      },
      {
        path: '/warehouse/addWarehouse',
        component: () => import('@/views/warehouse/warehouseInfo'),
        name: 'addWarehouse',
        meta: { title: 'addWarehouse',  roles: ['admin'], noCache: true},
        hidden: true,
      },
      {
        path: '/warehouse/editWarehouse',
        component: () => import('@/views/warehouse/warehouseInfo'),
        name: 'editWarehouse',
        meta: { title: 'editWarehouse',  roles: ['admin'], noCache: true},
        hidden: true,
      },
      {
        path: '/flWarehouse/warehouseList',
        component: () => import('@/views/flWarehouse/warehouseList'),
        name: 'flWarehouseList',
        meta: { title: '国内仓列表', roles: ['admin']},
      },
      {
        path: '/flWarehouse/addFlWarehouseInfo',
        component: () => import('@/views/flWarehouse/warehouseInfo'),
        name: 'addFlWarehouseInfo',
        meta: { title: '添加国内仓', roles: ['admin'], noCache: true},
        hidden: true,
      },
      {
        path: '/flWarehouse/editFlWarehouseInfo',
        component: () => import('@/views/flWarehouse/warehouseInfo'),
        name: 'editFlWarehouseInfo',
        meta: { title: '编辑国内仓',  roles: ['admin'], noCache: true},
        hidden: true,
      },
    ]
  },
  {
    path: '/help',
    component: FullPage, // 使用新的布局组件
    hidden: true,
    redirect: '/help/helpDocument',
    meta: {title:'help',roles:['admin'], icon: 'circle'},
    children: [
      {
        name: 'DocumentList',
        path: '/help/helpDocument',
        component: () => import('@/views/help-document/index.vue'),
        meta: { title: '帮助中心', icon: '' ,roles:['admin'] },
        hidden: true
      }
    ]
  },
  // {
  //   path: '/help',
  //   component: FullPage,
  //   redirect: '/help/document',
  //   meta: {title:'帮助中心',roles:['admin']},
  //   hidden: true,
  //
  //   children: [
  //     {
  //       path: '/help/document',
  //       component: () => import('@/views/help-document/index.vue'),
  //       name: 'Document',
  //       meta: { title: '帮助中心', roles: ['admin']},
  //       hidden: true,
  //
  //     }
  //     ]
  // },

  // 数据统计 statistics
  {
    path: '/statistics',
    component: Layout,
    redirect: '/statistics/index',
    meta: {title:'dataManagement',roles:['admin']},
    children: [
      {
        path: '/statistics/incomeReport',
        component: () => import('@/views/statistics/incomeReport'),
        name: 'incomeReport',
        meta: { title: 'incomeStatement',  roles: ['admin']},
      },
      {
        path: '/statistics/storage',
        component: () => import('@/views/statistics/storage'),
        name: 'storage',
        meta: { title: 'warehouseData',  roles: ['admin']},
      },
      {
        path: '/statistics/settlementList',
        component: () => import('@/views/statistics/settlementList'),
        name: 'settlementList',
        meta: { title: 'settlementDoc',  roles: ['admin']},
      },
      {
        path: '/statistics/settlementInfo',
        component: () => import('@/views/statistics/settlementInfo'),
        name: 'settlementInfo',
        meta: { title: 'settlementDetails',  roles: ['admin'], noCache: true},
        hidden: true,
      },
    ]
  },
  // 用户管理 user
  {
    path: '/user',
    component: Layout,
    redirect: '/user/normalUserList',
    meta: {title:'user',roles:['admin']},
    children: [
      {
        path: '/user/normalUserList',
        component: () => import('@/views/user/userList'),
        name: 'normalUserList',
        meta: { title: 'normalUserList',  roles: ['admin'], icon: ''},
      },
      {
        path: '/user/invalidUserList',
        component: () => import('@/views/user/userList'),
        name: 'invalidUserList',
        meta: { title: 'invalidUserList',  roles: ['admin']},
      },
      {
        path: '/user/userList',
        component: () => import('@/views/user/userList'),
        name: 'userList',
        meta: { title: 'userList',  roles: ['admin']},
        hidden: true
      },
      {
        path: '/user/userInfo',
        component: () => import('@/views/user/userInfo'),
        name: 'userInfo',
        meta: { title: 'userDetails',  roles: ['admin'], noCache: true},
        hidden: true,
      },
      {
        path: '/user/rechargeRecord',
        component: () => import('@/views/user/rechargeRecord'),
        name: 'rechargeRecord',
        meta: { title: 'rechargeRecord',  roles: ['admin'], icon: ''},
      },
      {
        path: '/user/addUser',
        component: () => import('@/views/user/addUser'),
        name: 'addUser',
        meta: { title: 'addUser',  roles: ['admin'], icon: '',noCache: true},
        hidden: true,
      },

    ]
  },
  // 设置
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting/index',
    meta: {title:'setting',roles:['admin']},
    hidden: true,
    children: [
      {
        path: '/setting/index',
        component: () => import('@/views/setting/index'),
        name: '',
        meta: { title: 'setting',  roles: ['admin']},
        hidden: true
      },
    ]
  },
  // 运营管理
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/adminList',
    meta: {title:'operationManagement',roles:['admin']},
    children: [
      {
        path: '/admin/adminList',
        component: () => import('@/views/admin/adminList'),
        name: 'operationsList',

        meta: { title: 'operationsList',  roles: ['admin']},
      },
      {
        path: '/admin/employeesList',
        component: () => import('@/views/admin/employeesList'),
        name: 'employeesList',
        meta: { title: 'employeesList',  roles: ['admin']},
      },
      {
        path: '/admin/unbindAccountList',
        component: () => import('@/views/admin/unbindAccountList'),
        name: 'unbindAccountList',
        meta: { title: 'unbindAccountList',  roles: ['admin']},
      },
    ]
  },
  // 系统设置
  {
    path: '/systemSetting',
    component: Layout,
    redirect: '/systemSetting/index',
    meta: {title:'systemSetting',roles:['admin']},
    children: [
      {
        path: '/systemSetting/index',
        component: () => import('@/views/systemSetting/index'),
        name: 'systemSetting',
        meta: { title: 'systemSetting',  roles: ['admin']},
      },
    ]
  },



  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/index', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
